<?php
require("dbconfig.php");
require("./class/sqlHelper.php");
$db = new Opmysqli(HOST, USER, PASSWORD, DBNAME);
session_start();  //启动会话
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>添加购物车</title>	
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
	font-family:微软雅黑;
	font-size:16px;
	color:#036;
	}
.color_c
{
    color:red;
    margin-top:30px;
}
.cart_con{
  margin-top:30px;
}
</style>
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
	<div class="container">
    <?php
    if($resError = $db->msg_error())
    {
      echo "<div class=\"well color_c\">";
      echo $resError;
      echo "</div>";
      exit(0);
    }

    $chooseS = $_POST['choose'];
    $total = $_POST['append'];
    //执行查询，获取要购买的信息
    //select sizeCode, longth, width from clothes_parameter where id = 10023 and sizeCode = '185(XXXL)';
    $query1 = "select id, name, price, pic from clothes where id = {$_GET['id']}";
    $query2 = "select id_auto, sizeCode, longth, width from clothes_parameter where id = {$_GET['id']} and sizeCode = ".
              "'{$chooseS}'";

    $resArr = $db->getRowsArray($query1);
    $resArr1 = $db->getRowsArray($query2);

    if (!empty($resArr) && !empty($resArr1))
    {
      //读取要购买的信息
      //将结果保存在一个数组
      $arr1 = array();
      foreach ($resArr as $value) 
      {
         $arr1 = $value;
      }
      foreach ($resArr1 as $value)
      {
         $arr1['sizeCode'] = $value['sizeCode'];
         $arr1['longth'] = $value['longth'];
         $arr1['width'] = $value['width'];
         $arr1['id_auto'] = $value['id_auto'];

      }
      $id_tem = $arr1['id'].$arr1['id_auto'];

      //判断total的值，解决用户多次点击total所造成的不同total值
      //即重新计算total的值，覆盖原来的值
      if ( isset($_SESSION["shopList"][$id_tem]['total']) && $_SESSION["shopList"][$id_tem]['sizeCode'] == $chooseS)
      {
        $arr1['total'] = $_SESSION["shopList"][$id_tem]['total']+$total;
      }
      else
      {
        $arr1['total'] = $total;
      }
     
      $_SESSION["shopList"][$id_tem] = $arr1;
      echo "<div class=\"alert cart_con\">";
         echo "<strong>添加购物车成功！</strong>";
      echo "</div>";
    
    }
    else
    {
        echo "<div class=\"well color_c\">";
        echo "sorry，没有找到你要购买的信息。";
        echo "</div>";
        exit(0);
    }
    $db->close_conn();
    ?>
  </div>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="./js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function Go()
{
  window.location="homePage.php";
}      
setTimeout("Go()", 1000);
</script>
</body>
</html>
    



